package com.grm.task;

import com.grm.util.SpringContextUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;

@Slf4j
@Setter
public class ScheduledTask implements Job {

    private Integer id;

    private String params;

    private String serviceBeanName;

    private String methodName;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        try {
            //使用反射执行spring中的任务
            ApplicationContext wac = SpringContextUtil.getApplicationContext();
            Object obj = wac.getBean(serviceBeanName);
            Class<?> serviceBean = wac.getBean(serviceBeanName).getClass();
            if (StringUtils.isNotEmpty(params)){
                Method method = serviceBean.getDeclaredMethod(methodName,String.class);
                method.invoke(wac.getBean(serviceBean),params);
            }else {
                Method method = serviceBean.getDeclaredMethod(methodName,null);
                method.invoke(wac.getBean(serviceBean),null);
            }
        }catch (Exception e){
            log.error("execute task error",e);
        }

    }
}

